package com.feeyo.monitor.rest;

import com.feeyo.monitor.domain.SimRecharge;
import com.feeyo.monitor.domain.Vehicle;
import com.feeyo.monitor.domain.VehicleSim;
import com.feeyo.monitor.service.SimRechargeService;
import com.feeyo.monitor.service.VehicleSimService;
import com.feeyo.monitor.utils.DateUtils;
import com.feeyo.monitor.utils.GloabalConstant;
import com.feeyo.monitor.utils.ResultData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author <a href="mailto:bliulei@variflight.com">liulei</a>
 * @Description Created by liulei on 2017/5/15 下午6:02.
 */
@RestController
public class VehicleSimController {
    @Autowired
    VehicleSimService vehicleSimService;
    @Autowired
    SimRechargeService simRechargeService;

    /**
     * @Description 分页获取sim列表
     * @params
     * @return
     */
    @RequestMapping(value="/manager/sims", method= RequestMethod.GET)
    public ResultData<PageInfo> getSims(@RequestParam(value="operator",required=false) String operator,
                                                @RequestParam(value="status",required=false) Integer status,
                                                @RequestParam(value="number",required=false) String number,
                                                @RequestParam(value="pageNumber",required=true) int pageNumber,
                                                @RequestParam(value="pageSize",required=true) int pageSize){
        ResultData<PageInfo> rd = new ResultData<PageInfo>();
        Map<String,Object> param = new HashMap<String,Object>();
        PageInfo page = null;
        if (operator != null)
            param.put("operator",operator);
        if (status != null) {
            param.put("status", status);
            param.put("invalidtime", DateUtils.getStandardDateStr(new Date()));
        }
        if (number != null)
            param.put("number",number);
        try {
            page = vehicleSimService.selectByPage(param, pageNumber, pageSize);
        }catch(Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
        }
        rd.setCode(GloabalConstant.SUCCESS);
        rd.setData(page);
        return rd;
    }

    /**
     * @Description 获取单个sim卡对象
     * @params
     * @return
     */
    @RequestMapping(value="/manager/sims/{simId}", method= RequestMethod.GET)
    public ResultData<VehicleSim> getSim(@PathVariable(value="simId",required=true) Integer simId){
        ResultData<VehicleSim> rd = new ResultData<VehicleSim>();
        VehicleSim vehicleSim = null;
        try {
            vehicleSim = vehicleSimService.selectByPrimaryKey(simId);
        }catch (Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
        }
        rd.setCode(GloabalConstant.SUCCESS);
        rd.setData(vehicleSim);
        return rd;
    }

    /**
     * @Description 删除sim卡对象
     * @params
     * @return
     */
    @RequestMapping(value="/manager/sims/delete/{simId}", method= RequestMethod.GET)
    public ResultData<Object> delete(@PathVariable(value="simId",required=true) Integer simId){
        ResultData<Object> rd = new ResultData<Object>();
        try{
            vehicleSimService.updateDeleteStatus(simId);
        }catch(Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        return rd;
    }

    /**
     * @Description 新建或修改sim对象
     * @params
     * @return
     */
    @RequestMapping(value="/manager/sims", method=RequestMethod.POST)
    public ResultData<Object> saveSim(@ModelAttribute("vehicleSim") VehicleSim vehicleSim){
        ResultData<Object> rd = new ResultData<Object>();
        try {
            if (vehicleSim.getId() != null)
                vehicleSimService.updateByPrimaryKeySelective(vehicleSim);
            else {
                int result = vehicleSimService.insert(vehicleSim);
                if (vehicleSim.getId() >0) {
                    if (vehicleSim.getPaymoney() != null){
                        SimRecharge simRecharge = new SimRecharge();
                        simRecharge.setSimId(vehicleSim.getId());
                        simRecharge.setPayTime(vehicleSim.getPaytime());
                        simRecharge.setInvalidTime(vehicleSim.getInvalidtime());
                        simRecharge.setPayMoney(vehicleSim.getPaymoney());
                        simRechargeService.insert(simRecharge);
                    }
                }
            }
        }catch (Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        return rd;
    }

    @RequestMapping(value="/manager/simsbatch", method=RequestMethod.POST)
    public ResultData<Object> saveSimBatch(@RequestParam(value="ids",required=true) String ids,
                                           @RequestParam(value="paytime",required=false) String paytime,
                                           @RequestParam(value="invalidtime",required=false) String invalidtime,
                                           @RequestParam(value="paymoney",required=false) Double paymoney){
        ResultData<Object> rd = new ResultData<Object>();
        Map<String,Object> param = new HashMap<String,Object>();
        List<String> idList = Arrays.asList(ids.split(","));
        List<Integer> idListInt = new ArrayList<>();
        for(String id : idList)
            idListInt.add(Integer.parseInt(id));
        param.put("ids",idListInt);

        if (paytime != null)
            param.put("paytime",paytime);
        if (invalidtime != null)
            param.put("invalidtime",invalidtime);
        if (paymoney != null)
            param.put("paymoney",paymoney);
        try{
            vehicleSimService.updateByBatch(param);
        }catch(Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        return rd;
    }

    /**
     * @Description sim卡充值记录
     * @params 
     * @return 
     */
    @RequestMapping(value="/manager/sim/pay/{simId}", method= RequestMethod.GET)
    public ResultData<List<SimRecharge>> simRecharge(@PathVariable(value="simId",required=true) Integer simId){
        ResultData<List<SimRecharge>> rd = new ResultData<List<SimRecharge>>();
        List<SimRecharge> result = null;
        try{
            result = simRechargeService.selectBySim(simId);
        }catch(Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        rd.setData(result);
        return rd;
    }

    /**
     * @Description 添加SIM卡充值记录
     * @params
     * @return 
     */
    @RequestMapping(value="/manager/sim/pay", method= RequestMethod.POST)
    public ResultData<Object> saveSimRecharge(@ModelAttribute("simRecharge")  SimRecharge simRecharge){
        ResultData<Object> rd = new ResultData<Object>();
        try{
            simRechargeService.insert(simRecharge);
        }catch(Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        return rd;
    }

    /**
     * @Description 日期参数提交转换
     * @params
     * @return
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
